package com.example.weddingapp.activity;

import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.AndroidViewModel;

import com.example.weddingapp.R;
import com.example.weddingapp.base.BaseActivity;
import com.example.weddingapp.base.BaseObserver;
import com.example.weddingapp.databinding.ActivityValueAddBinding;

import java.util.List;

import cn.leancloud.AVObject;
import cn.leancloud.AVQuery;
import cn.leancloud.AVUser;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ValueAddActivity extends BaseActivity<AndroidViewModel, ActivityValueAddBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_value_add);
        showContentView();
        setTitle("充值");


    }

    @Override
    protected void initView() {
        super.initView();
        bindingView.btnValueAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }


    private void submit() {
        final AVUser user = AVUser.getCurrentUser();
        String s = bindingView.etMoney.getText().toString();
        try {
            Double value = Double.valueOf(s);
            if (value <= 0) {
                showToast("金额必须大于0！");
                return;
            }
            if (value > 100000) {
                showToast("上限为10万！");
                return;
            }
            //分
            value = value * 100;
            final int intValue = value.intValue();
            //查询是否有金钱账户
            AVQuery<AVObject> query = new AVQuery<>("Account");
            query.whereEqualTo("user", user);
            query.findInBackground().subscribe(new Observer<List<AVObject>>() {

                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(List<AVObject> list) {
                    if (list.size() == 0) {
                        //Account表不存在，添加新数据
                        AVObject account = new AVObject("Account");
                        account.put("amount", intValue);
                        account.put("user", user);
                        account.saveInBackground().subscribe(new BaseObserver<AVObject>() {
                            @Override
                            public void onNext(AVObject avObject) {
                                super.onNext(avObject);
                                AVObject object = new AVObject("Order");
                                object.put("action", "充值");
                                object.put("fromUser", user);
                                object.put("intMoney", intValue);
                                object.saveInBackground().subscribe(new BaseObserver<AVObject>(ValueAddActivity.this) {
                                    @Override
                                    public void onNext(AVObject avObject) {
                                        super.onNext(avObject);
                                        showToast("充值成功");
                                        finish();
                                    }
                                });
                            }
                        });
                    } else {
                        //Account表存在，更新数据
                        list.get(0).increment("amount", intValue);
                        list.get(0).saveInBackground().subscribe(new BaseObserver<AVObject>() {
                            @Override
                            public void onNext(AVObject avObject) {
                                super.onNext(avObject);

                                AVObject object = new AVObject("Order");
                                object.put("action", "充值");
                                object.put("fromUser", user);
                                object.put("intMoney", intValue);
                                object.saveInBackground().subscribe(new BaseObserver<AVObject>(ValueAddActivity.this) {
                                    @Override
                                    public void onNext(AVObject avObject) {
                                        super.onNext(avObject);
                                        showToast("充值成功");
                                        finish();
                                    }
                                });
                            }
                        });
                    }

                }

                @Override
                public void onError(Throwable e) {
                    //Account表不存在，添加新数据
                    AVObject account = new AVObject("Account");
                    account.put("amount", intValue);
                    account.put("user", user);
                    account.saveInBackground().subscribe(new BaseObserver<AVObject>() {
                        @Override
                        public void onNext(AVObject avObject) {
                            super.onNext(avObject);
                            AVObject object = new AVObject("Order");
                            object.put("action", "充值");
                            object.put("fromUser", user);
                            object.put("intMoney", intValue);
                            object.saveInBackground().subscribe(new BaseObserver<AVObject>(ValueAddActivity.this) {
                                @Override
                                public void onNext(AVObject avObject) {
                                    super.onNext(avObject);
                                    showToast("充值成功");
                                    finish();
                                }
                            });
                        }
                    });
                }

                @Override
                public void onComplete() {

                }
            });

        } catch (Exception e) {
            showToast("金额输入有误！");
        }


    }
}
